package com.atguigu.yygh.hosp.service.impl;

import com.atguigu.yygh.hosp.dao.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
@Service
public class DepartmentServiceImpl  implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void saveDepartment(Department department) {
       Department mongoDepartment= departmentRepository.findByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
       if(mongoDepartment == null){//添加
           department.setCreateTime(new Date());
           department.setUpdateTime(new Date());
           department.setIsDeleted(0);
           departmentRepository.save(department);
       }else{//修改
           department.setCreateTime(mongoDepartment.getCreateTime());
           department.setUpdateTime(new Date());
           department.setIsDeleted(mongoDepartment.getIsDeleted());

           department.setId(mongoDepartment.getId());
           departmentRepository.save(department);
       }
    }

    //查询科室信息
    @Override
    public Page<Department> getDepartmentPage(String hoscode, Integer page, Integer limit) {
        Department department=new Department();
        department.setHoscode(hoscode);
        //添加分页条件
        department.setIsDeleted(0);
        Example<Department> example=Example.of(department);

        return departmentRepository.findAll(example, PageRequest.of(page-1, limit));
    }

    /*
    删除科室信息
     */
    @Override
    public void removeDepartment(String hoscode, String depcode) {
        //科室信息=先查出医院信息和科室信息
        Department department = departmentRepository.findByHoscodeAndDepcode(hoscode, depcode);
        //将状态设置为1
        department.setIsDeleted(1);
        //跟新修改时间
        department.setUpdateTime(new Date());
        //跟新
        departmentRepository.save(department);
    }

    //查询科室信息,返回有层级结构的数据给前端
    @Override
    public List<DepartmentVo> findAll(String hoscode) {
        List<Department> list = departmentRepository.findByHoscode(hoscode);//返回所有的科室列表
        //key:当前科室所在大科室的编号 value:科室列表
        Map<String, List<Department>> collect = list.stream().collect(Collectors.groupingBy(Department::getBigcode));

        Set<Map.Entry<String, List<Department>>> entries = collect.entrySet();
        List<DepartmentVo> bigDepartmentList = new ArrayList<DepartmentVo>();

        for (Map.Entry<String, List<Department>> entry : entries) {
            DepartmentVo departmentVo = new DepartmentVo();
            String bigCode = entry.getKey();//键是大科室编号
            String bigname = entry.getValue().get(0).getBigname();
            List<Department> value = entry.getValue();
            List<DepartmentVo> smallDepartmentVoList = new ArrayList<DepartmentVo>();
            for (Department department : value) {
                DepartmentVo smallDeptVo = new DepartmentVo();
                smallDeptVo.setDepcode(department.getDepcode()); //设置子科室编号
                smallDeptVo.setDepname(department.getDepname()); //设置子科室名字
                smallDepartmentVoList.add(smallDeptVo);
            }

            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(bigname);
            departmentVo.setChildren(smallDepartmentVoList);
            bigDepartmentList.add(departmentVo);
        }
        return bigDepartmentList;
    }
}
